<?php
$hope_upload = new Hope_upload();
CLass Hope_upload{

	public function __construct(){
	
		add_action('wp_ajax_hope_upload', array( $this, 'hope_upload_img' ));
	}
	
	public function hope_upload_img(){
		$upload_dir = wp_upload_dir();
		//print_r($_FILES);
		$allowedExts = array("jpg", "jpeg", "gif", "png");
		$extension = end(explode(".", $_FILES["file"]["name"]));
		if ((($_FILES["file"]["type"] == "image/gif")
		|| ($_FILES["file"]["type"] == "image/jpeg")
		|| ($_FILES["file"]["type"] == "image/pjpeg"))
		&& ($_FILES["file"]["size"] < 20000)
		&& in_array($extension, $allowedExts))
		  {
		  if ($_FILES["file"]["error"] > 0)
			{
			echo $_FILES["file"]["error"] . "<br />";
			}
		  else
			{	
			if (file_exists($upload_dir[ 'path' ] . "/" . $_FILES["file"]["name"]))
			  {
			  $_FILES["file"]["name"] = $_FILES["file"]["name"].rand(0,1000); 
			  }
			else
			  {
				move_uploaded_file($_FILES["file"]["tmp_name"],
				$upload_dir['path'] . "/" . $_FILES["file"]['name']);
				echo "<img src=".$upload_dir['url'] . "/" . $_FILES["file"]['name']." />";		
			  }
			}
		  }
		else
		  {
		  echo "Invalid file";
		  }
		die();  
	}
}
?>